//FSM控制器，用于控制玩家和计算机如何玩TIC TAC玩具游戏

module fsm_controller(
     input clock,// 时钟
     input reset,// 复位 
     play, // 玩家 
     pc,// 电脑
     illegal_move,// 非法移动信号 
     no_space, // 没位置信号 
     win, // 胜利信号 
    //  output reg computer_play, // 电脑使能输出 
    //  output reg player_play,   // 玩家使能输出 
     output reg even_score_flag // 平局
     );
// FSM States 
  parameter IDLE=2'b00;
  parameter PLAYER=2'b01;
  parameter COMPUTER=2'b10;
  parameter GAME_DONE=2'b11;
  reg[1:0] current_state, next_state;
// //current state registers 
//   always @(posedge clock or negedge reset) 
//     begin 
//       if(!reset)
//         current_state <= IDLE;
//       else 
//         current_state <= next_state;
//     end 
//   // next state 
//   always @(*)
//   begin
//     case(current_state)
//         IDLE: begin 
//           if(reset==1'b1 && play == 1'b1)
//             next_state <= PLAYER; // player to play 
//           else 
//             next_state <= IDLE;
//             player_play <= 1'b0;
//             computer_play <= 1'b0;
//             even_score_flag <= 1'b0;
//         end 
//         PLAYER:begin 
//           player_play <= 1'b1;
//           computer_play <= 1'b0;
//           if(illegal_move==1'b0)
//             next_state <= COMPUTER; // computer to play 
//           else 
//             next_state <= IDLE;
//         end 
//         COMPUTER:begin 
//           player_play <= 1'b0;
//           if(pc==1'b0) begin 
//             next_state <= COMPUTER;
//             computer_play <= 1'b0;
//           end
//           else if(win==1'b0 && no_space == 1'b0)
//           begin 
//             next_state <= IDLE;
//             computer_play <= 1'b1;// computer to play when PC=1
//           end 
//           else if(no_space == 1 || win ==1'b1)
//           begin 
//             next_state <= GAME_DONE; // game done 
//             computer_play <= 1'b1;// computer to play when PC=1
//           end  
//         end 
//         GAME_DONE:begin // game done
//           player_play <= 1'b0;
//           computer_play <= 1'b0; 
//           if(reset==1'b0) 
//             next_state <= IDLE;// reset the game to IDLE  
//           else 
//             next_state <= GAME_DONE;  
//         end 
//        default: next_state <= IDLE; 

//     endcase

//   end
always @(posedge clock or negedge reset) 
    begin 
      if(!reset)
        even_score_flag <= 1'b0;
      else  if(no_space==1'b1 && win==1'b0)
        even_score_flag <= 1'b1;
      else
        even_score_flag  <= even_score_flag;
    end 
 

endmodule 